<template>
  <div>
    <div class="goods-list">
  <!--    <router-link tag="div" :to="'/home/goodsinfo/'+item.id" class="goods-item" v-for="item in goodslist"
                   :key="item.id">
        <img :src="item.img_url" alt="" @error="imgError(item)">
        <h1 class="title">{{item.title}}</h1>
        <div class="info">
          <p class="price">
            <span class="now">￥{{item.sell_price}}</span>
            <span class="old">￥{{item.market_price}}</span>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span>剩{{item.stock_quantity}}件</span>
          </p>
        </div>
      </router-link>-->
      <!--在网页中有两种跳转方式
      1. 使用a标签跳转
      2. 使用window.location.href 编程式导航-->
      <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="goDetail(item.id)">
        <img :src="item.img_url" alt="" @error="imgError(item)">
        <h1 class="title">{{item.title}}</h1>
        <div class="info">
          <p class="price">
            <span class="now">￥{{item.sell_price}}</span>
            <span class="old">￥{{item.market_price}}</span>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span>剩{{item.stock_quantity}}件</span>
          </p>
        </div>
      </div>
    </div>
    <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
  </div>
</template>

<script>
  import {Toast} from 'mint-ui';

  export default {
    data() {
      return {
        pageindex: 1,//默认展示第一页
        goodslist: []//存放商品列表的数组
      }
    },
    created() {
      this.getGoodsList();
    },
    methods: {
      getGoodsList() {
        //获取商品列表
        this.$http.get('api/getgoods?pageindex=' + this.pageindex)
          .then(result => {
            if (result.body.status === 0) {
              this.goodslist = this.goodslist.concat(result.body.message);
            } else {
              Toast('获取商品列表失败');
            }
          })
      },
      getMore() {
        this.pageindex++;
        this.getGoodsList();
      },
      goDetail(id) {
        //使用router.push
        /*注意：$route 和 $router的区别*/
        /*$route 是路由参数对象 params*/
        /*$router 是路由导航对象 使用js代码实现路由 前进 后退和跳转*/
        // this.$router.push('/home/goodsinfo/'+id);
        // this.$router.push({path: '/home/goodsinfo/' + id});
        this.$router.push({
          name: 'goodsinfo',
          params: {id: id}
        });
      },
      imgError(item) {
        //图片加载失败的默认图片
        item.img_url = 'http://img.mp.sohu.com/upload/20170715/7b3ce9cd49604308bdd2b7755f52cab9_th.png';
      }
    }
  }
</script>

<style lang="scss" scoped>
  .goods-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 5px;

    .goods-item {
      width: 49%;
      border: 1px solid #ccc;
      box-shadow: 0 0 5px #999;
      margin: 3px 0;
      padding: 2px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      min-height: 293px;

      img {
        width: 100%;
      }

      .title {
        font-size: 14px;
      }

      .info {
        background-color: #eee;

        p {
          margin: 0;
          padding: 5px;
        }

        .price {
          .now {
            color: red;

            font-size: 16px;
          }

          .old {
            margin-left: 10px;
            font-size: 12px;
            text-decoration: line-through;
          }
        }

        .sell {
          display: flex;
          justify-content: space-between;
          font-size: 13px;
        }
      }
    }
  }
</style>